(function () {
    var serverData = {};
    var serverTable;
    var serverData1 = {};
    var serverTable1;
    //查询
    jQuery('#search_btn').on('click',function(){
        serverData.key=jQuery('#search').val();
        serverTable.draw();
    });
    //点击新增加载dialog中的内容
    jQuery('.addBtn').on('click',function(){
    	jQuery("#modal-find-auditor").modal('show');
    	serverTable = jQuery('#table-user').DataTable({
    		"serverSide": true,
    		"ordering": false,
    		"ajax": {
    			"dataType": "json",
    			"type": "get",
    			"cache": false,
    			"url":window.rootPath+ '/ui/customerAllocationIndex/getpage',
    			"data": function (d) {
    				return $.extend({}, d, serverData);
    			}
    		},
    	    "columns" : [
			{
				data : null,
                defaultContent : ""// 排序   1
			},
            {
	            data: null,
	            defaultContent: "",
	            render: function (data, type, full, meta) {
	            	var id=data.id
	            	var operate ='<input type="checkbox" class="checkchildContract">';
	            	return operate;
	            }   
            },{
			    data : 'name'//姓名
			},
            
            {// 3性别
				data : 'sex'
			},

			{// 4职务
				data : 'position'
			}
			],
    "drawCallback": function (oSettings) {
        var k = 0;
        var total = oSettings.aiDisplay.length + oSettings._iDisplayStart;
        for (var i = oSettings._iDisplayStart, iLen = total; i < iLen; i++) {
            $('td:eq(0)', oSettings.aoData[oSettings.aiDisplay[k]].nTr).text(i + 1);
            k++;
        }
    }
});

    });
    //默认加载列表
    jQuery(document).ready(function () {
    	serverData.key ='';
    	serverTable1 = jQuery('#customerTable').DataTable({
    		"serverSide": true,
    		"ordering": false,
    		"ajax": {
    			"dataType": "json",
    			"type": "get",
    			"cache": false,
    			"url":window.rootPath+ '/ui/customerAllocationIndex/Allgetpage',
    			"data": function (d) {
    				return $.extend({}, d, serverData1);
    			}
    		},
    	    "columns" : [
				{
					data : null,
                    defaultContent : ""// 排序   1
				},
    	      {
	            data: null,
	            defaultContent: "",
	            render: function (data, type, full, meta) {
	            	var id=data.id
	            	var operate ='<input type="checkbox" class="checkchildContract">';
	            	return operate;
	            }   
            },
            {
			    data : 'name'//姓名
			},
            {// 3性别
				data : 'sex'
			},

			{// 4职务
				data : 'position'
			}
			],
    "drawCallback": function (oSettings) {
        var k = 0;
        var total = oSettings.aiDisplay.length + oSettings._iDisplayStart;
        for (var i = oSettings._iDisplayStart, iLen = total; i < iLen; i++) {
            $('td:eq(0)', oSettings.aoData[oSettings.aiDisplay[k]].nTr).text(i + 1);
            k++;
        }
    }
    		
    		
    	});
    });
    
   /**
    * 新增dialog中的引入按钮
    */
    jQuery("#btn-reference").on('click',function() {
    	var selects=getSelect("#table-user",1);
    	if(selects.length==0){
    		$.springLemonDialog({
   	         content: '请选择要操作的数据',// 确认文本
   	         clickButton: function (sender, modal, index) {// 按钮事件处理
   	             $(this).closeDialog(modal);
   	         }
   	       }); 
    	}else
    	if(selects.length >1){
			$.springLemonDialog({
   	         content: '每次只能操作一条信息！',// 确认文本
   	         clickButton: function (sender, modal, index) {// 按钮事件处理
   	             $(this).closeDialog(modal);
   	         }
   	       }); 
		return;
		}
    	if(selects.length == 1){
    		var name =  selects[0].name;
    		var sex = selects[0].sex;
    		var userId = selects[0].id;
    		var position = selects[0].position;
    		url = window.rootPath+ '/ui/customerAllocationIndex/saveCustomerAllocation';
    		var d={
					"name" : name,
					"sex" : sex,
					//"userId" : userId,
					"position" : position,
				};
			 jQuery.ajax({
					url : encodeURI(url),
					type : 'post',
					data : JSON.stringify(d),
					dataType : 'json',
					contentType : 'application/json;charset=utf-8',
					success : function(data) {
						jQuery("#modal-find-auditor").modal('hide');
						serverTable1.draw();
					}
				}); 
    	}
    	
    });
    
    jQuery(".delBtn").on('click',function() {
    	var selects=getSelect("#customerTable",1);
    	if(selects.length==0){
    		$.springLemonDialog({
   	         content: '请选择要操作的数据',// 确认文本
   	         clickButton: function (sender, modal, index) {// 按钮事件处理
   	             $(this).closeDialog(modal);
   	         }
   	       }); 
    	}else
    	if(selects.length >1){
			$.springLemonDialog({
   	         content: '每次只能操作一条信息！',// 确认文本
   	         clickButton: function (sender, modal, index) {// 按钮事件处理
   	             $(this).closeDialog(modal);
   	         }
   	       }); 
		return;
		}
    	if(selects.length == 1){
    		var name =  selects[0].name;
    		var sex = selects[0].sex;
    		var userId = selects[0].id;
    		var position = selects[0].position;
    		url = window.rootPath+ '/ui/customerAllocationIndex/delCustomerAllocation?id='+userId;
    		var d={
					"name" : name,
					"sex" : sex,
					//"userId" : userId,
					"position" : position,
				};
			 jQuery.ajax({
					url : encodeURI(url),
					type : 'post',
					data : JSON.stringify(d),
					dataType : 'json',
					contentType : 'application/json;charset=utf-8',
					success : function(data) {
						//jQuery("#modal-find-auditor").modal('hide');
						serverTable1.draw();
					}
				}); 
    	}
    });
    /**
     * dialog中的查询按钮
     * */
    jQuery('#btn-searchbtn').on('click',function(){
        serverData.key=jQuery('#search1').val();
        serverTable.draw();
    })
    /**
     * dialog中的撤销按钮
     * */
    jQuery('#btn-resetbtn').on('click',function(){
        serverData.key="";
        serverTable.draw();
    })
})();





